|
|
|
|
|
|
|
In Visual Basic, you use mathematical operations to create equations. These equations can include multiple operators, variables, and expressions, as in the following example: |
|
|
|
|
|
|
|
|
(115 + txtAmount.Text) / 69 * 1.0825 |
|
|
|
|
|
|
|
|
Note that although the sample expression is valid, the preceding line of code does not do anything by itself. While you're reading, keep in mind that when you use these operators, the result will be going somewhere; for example, it may be assigned to a variable or displayed on the screen. |
|
|
|
|
|
|
|
|
The two simplest math operations are addition and subtraction. If you have ever used a calculator to do addition and subtraction, you already have a good idea how these operations are performed in a line of computer code. |
|
|
|
|
|
|
|
|
A computer program, however, gives you greater flexibility in the operations you can perform than a calculator does. Your programs are not limited to working with literal numbers (for example, 1, 15, 37.63, 105.2). Your program can add or subtract two or more literal numbers, numeric variables, or any functions that return a numeric value. Also, as with a calculator, you can perform addition and subtraction operations in any combination. Now take a look at exactly how you perform these operations in your program. |
|
|
|
|
|
|
|
|
Using the Addition Operator |
|
|
|
|
|
|
|
|
The operator for addition in Visual Basic is the plus sign (+). The general use of this operator is shown in the following syntax line: |
|
|
|
|
|
|
|
|
result = number1 + number2 [+ number3] |
|
|
|
|
|
|
|
|
result is a variable (or control property) that contains the sum of the numbers. The equal sign indicates the assignment of a value to the variable. number1,number2, and number3 are the literal numbers, numeric variables, or functions that are to be added together. You can add as many numbers together as you like, but each number pair must be separated by a plus sign. |
|
|
|
|
|
|
|
|
Using the Subtraction Operator |
|
|
|
|
|
|
|
|
The operator for subtraction is the minus sign (-). The syntax is basically the same as for addition: |
|
|
|
|
|
|
|
|
result = number1 - number2 [- number3] |
|
|
|
|
|
|
|
|
Although the order does not matter in addition, in subtraction, the number to the right of the minus sign is subtracted from the number to the left of the sign. If you have multiple numbers, the second number is subtracted from the first, then the third number is subtracted from that result, and so on, moving from left to right. For example, consider the following equation: |
|
|
|
|
|
|
|
|
The computer first subtracts 6 from 15 to yield 9. It then subtracts 3 from 9 to yield 6, which is the final answer stored in the variable result. |
|
|
|
|
|